package com.thkj.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.thkj.entity.TblIndividual;
import com.thkj.entity.TblInformation;
import com.thkj.entity.TblSecurityAdmin;
import com.thkj.service.TblIndividualService;
import com.thkj.service.TblInformationService;
import com.thkj.service.TblSecurityAdminService;
import com.thkj.util.PushUtils;
import com.thkj.util.RequestUtil;
import com.thkj.util.RmUtil;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import dp.R;
import dp.dao.PagingList;
import com.thkj.framework.base.BaseController;

@RestController
@RequestMapping("/backstage/information")
public class InformationAction extends BaseController {
    @Autowired
    TblInformationService tblInformationService;
    @Autowired
    TblIndividualService tblIndividualService;
    @Autowired
    TblSecurityAdminService tblSecurityAdminService;
    
    
    @RequestMapping("")
    public ModelAndView index(){
    	return new ModelAndView("information/list");
    }
	
	@RequestMapping("/query")
    public R query(HttpServletRequest request, int page, int size){
    	Map<String, Object> param = RequestUtil.getParameterMap(request);
    	PagingList<Map> list = tblInformationService.query(page, size,param);
    	return R.success(list);
    }
	
    @RequestMapping("/edit")
    public R edit(HttpServletRequest request, TblInformation information){
    	try {
    		if(StrUtil.isBlank(information.getId())) {
    			information.setId(IdUtil.objectId());
    			information.setUserId(RmUtil.getLoginUser(request).getUserId());
    			tblInformationService.insertTblInformation(information);
        	}else {
        		tblInformationService.updateTblInformation(information);
        	}
        	return R.SUCCESS;
    	}catch (Exception e) {
			return R.failure(e.getMessage());
		}
    }
    
    @RequestMapping("/delete")
    public R delete(String id){
    	tblInformationService.deleteTblInformation(id);
    	return R.SUCCESS;
    }
    
    @RequestMapping("/push")
    public R push(String id){
    	TblInformation information = tblInformationService.getTblInformationById(id);
    	TblIndividual individual = tblIndividualService.getTblIndividualById(information.getIndividualId());
    	TblSecurityAdmin securityAdmin = tblSecurityAdminService.getTblSecurityAdminById(individual.getUserId());
    	if(individual!=null && securityAdmin!=null) {
    		PushUtils.sendForJg(securityAdmin.getRegistrationId(), "jy", information);
    		return R.SUCCESS;
    	}else {
    		return R.failure("用户不存在！");
    	}
    }
}